<template>
  <div>
    <van-nav-bar title="登录" />
    <!-- form表单 -->
    <van-form
      ref="loginForm"
      style="background-color: #ededed"
      @submit="onSubmit"
    >
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="jiaoYan.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="iconfont icon-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="jiaoYan.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <van-count-down
            v-if="falg"
            :time="1000 * 3"
            format="ss s"
            @finish="falg = false"
          />
          <van-button
            v-else
            style="background-color: #07c160; color: #fff; border-radius: 5px"
            native-type="button"
            @click="myBtn"
            size="small"
            type="default"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button
          style="background-color: #6db4fb; border: #6db4fb"
          block
          type="info"
          native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <!-- <div class="loginIs">账号:13911111111 密码:246810</div>
    <div class="loginIs">如果收不到验证码,请使用万能验证码:246810</div> -->
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
export default {
  name: 'my-login',
  data () {
    return {
      falg: false,
      user: {
        mobile: '13911111111',
        code: '246810'
      },
      jiaoYan: {
        mobile: [
          { required: true, message: '请输入手机号' },
          {
            pattern: /^1[3|5|7|8]\d{9}$/,
            message: '手机号格式错误'
          }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式不正确'
          }
        ]
      }
    }
  },

  created () {},

  methods: {
    async onSubmit () {
      // 轻提示
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true, // 禁用背景点击
        duration: 0
      })
      try {
        const { data } = await login(this.user)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功')
        // console.log('登录成功')
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('验证码或手机号错误')
        } else {
          this.$toast.fail('登录失败,稍后重试')
        }
      }
    },
    async myBtn () {
      console.log('myBtn')
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log('验证失败', err)
      }
      this.falg = true
      try {
        const res = await sendSms(this.user.mobile)
        console.log('发送成功', res)
      } catch (err) {
        this.falg = false
        if (err.response.status === 429) {
          this.$toast('发送过于频繁,稍后重试')
        }
        this.$toast('发送失败,稍后重试')
        // console.log('发送失败', err)
      }
    }
  }
}
</script>

<style scoped lang='less'>
// .loginIs {
//   background-color: pink;
//   font-size: 12px;
//   margin:0 auto;
//   width: 100%;
// }
</style>
